package com.atguigu.day2;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Res {
    private int number = 50;
    Lock lock = new ReentrantLock(true);//Lock默认是false即非公平锁

    public void sale() {
        lock.lock();
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "已卖出第" + (number--) + "还剩下第" + number);
            }
        } finally {
            lock.unlock();
        }

    }


    public synchronized void m1() {
        System.out.println(Thread.currentThread().getName() + "=======>come in");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    public void m2() {
        if (lock.tryLock()) {
            try {
                System.out.println(Thread.currentThread().getName() + "=======>come in");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            } finally {
                lock.unlock();
            }

        } else {
            System.out.println(Thread.currentThread().getName() + "------> lock used........,leave out");
        }


    }


    public void m3() throws InterruptedException {
        if (lock.tryLock(4, TimeUnit.SECONDS)) {
            try {
                System.out.println(Thread.currentThread().getName() + "=======>come in");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            } finally {
                lock.unlock();
            }

        } else {
            System.out.println(Thread.currentThread().getName() + "------> lock used........,leave out");
        }


    }
}


public class SynchronizedVSLock {
    /*
     * Synchronized VS LOCK
     *
     *Synchronized
     *  关键字
     *  独占
     * 非公平
     * 可重入锁
     *
     *
     * Lock
     * 类
     * 有超时等待，中途退出机制
     * 独占可公平可非公平
     * 可重入锁
     *
     * */
    public static void main(String[] args) throws InterruptedException {
        //案例一公平锁和非公平锁演示
        /*口诀一：高内低耦前提下 线程 操作 资源*/
//        Res res = new Res();
//        /*lambda 口诀：拷贝小括号 写死右箭头 落地大括号*/
//        new Thread(() -> {
//            for (int i = 0; i < 50; i++) {
//                res.sale();
//            }
//        }, "A").start();
//
//        new Thread(() -> {
//            for (int i = 0; i < 50; i++) {
//                res.sale();
//            }
//        }, "B").start();
//
//        new Thread(() -> {
//            for (int i = 0; i < 50; i++) {
//                res.sale();
//            }
//        }, "C").start();

        Res res = new Res();
        //案例二 演示LOCK锁超时等待，中途退出取消机制
        new Thread(() -> {
            try {
                res.m3();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t1").start();
        TimeUnit.MILLISECONDS.sleep(10);
        new Thread(() -> {
            try {
                res.m3();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t2").start();

    }
}

